#include "my_comtrade.h"
char buf[100];
//==========================
int write_file_gdb(int fd,char *src,int len,int *counter)
{
    if(fd==-1)
    {
        return -1;
    }
    u2g(src, len, buf, sizeof(buf));   //utf8转GDB2312,不转则打开中文乱码
    int  ret = write(fd, buf,strlen(buf));
    if (-1 == ret) 
    {
        printf("Error: write file failed!\n");
    }
    (*counter)+=(len); 
    return ret;
}
//================================================================
int write_file_utf(int fd,char *src,int len,int  *counter)
{
    if(fd==-1)
    {
        return -1;
    }
    int  ret = write(fd, src,strlen(src));
    if (-1 == ret) 
    {
        printf("Error: write file failed!\n");
    }
    return ret;
}
// #define write_file(fd,src,len,counter) do {                                                    \
//     char buf[100];                                                                                                         \
//     u2g(src, strlen(src), buf, sizeof(buf));                                                                    \
//     int ret = write(fd, buf,strlen(buf));                                                                                 \
//     if(ret == -1){                                                                                                              \
//         printf("Error: write file failed!\n");}                                                             \
//     (counter)+=(len);                                                                                                           \
//     }while(0);                                                                                                                      


                                     
//==========================字符串中转
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen,
		char *outbuf, size_t outlen) 
{
	// iconv_t cd;
	// char **pin = &inbuf;
	// char **pout = &outbuf;

	// cd = iconv_open(to_charset, from_charset);
	// if (cd == 0)
	// 	return -1;
	// memset(outbuf, 0, outlen);
	// if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
	// 	return -1;
	// iconv_close(cd);
	// *pout = '\0';

	// return 0;
    iconv_t cd;
    int rc;
    char **pin = &inbuf;
    char **pout = &outbuf;
     
    cd = iconv_open(to_charset,from_charset);
    if (cd==0) return -1;
    memset(outbuf,0,outlen);
    if (iconv(cd,pin,&inlen,pout,&outlen)==-1) return -1;
    iconv_close(cd);
    return 0;
}

int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
	return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}

int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
	return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
// ===================================

int write_cmtr_cfg_info(int fd,cmtr_cfg_info *cfg,int *counter)
{

    int strlenr = 0;

    int strmaxl = CMTR_STRING_MAX_LENGTH * 13;

    char* strline = (char *)malloc(strmaxl);
    int index = 0;
    int _fd = fd;
    memset(strline, '\0', strmaxl);
    strlenr = sprintf(strline, "%s,%s\n", cfg->station_name,cfg->kymograph_id);  
    write_file_gdb(fd,strline,strlenr,counter);
   
    memset(strline, '\0', strmaxl);
    strlenr = sprintf(strline, "%d,%dA,%dD\n", cfg->analog_count + cfg->digit_count, cfg->analog_count, cfg->digit_count);
    write_file_gdb(fd,strline,strlenr,counter);

    for (index = 0; index < cfg->analog_count; index++) {

                 memset(strline, '\0', strmaxl);

                 strlenr = sprintf(strline, "%d,%s,%s,%s,%s,%lf,%lf,%d,%d,%d,%d,%d,%s\n",

                                   cfg->analogs[index].index+1,

                                   cfg->analogs[index].name,

                                   cfg->analogs[index].phase,

                                   cfg->analogs[index].element,

                                   cfg->analogs[index].unit,

                                   cfg->analogs[index].factor_a,

                                   cfg->analogs[index].factor_b,

                                   cfg->analogs[index].offset_time,

                                   cfg->analogs[index].smp_min,

                                   cfg->analogs[index].smp_max,
                                   
                                   cfg->analogs[index].ct1,

                                   cfg->analogs[index].ct2,

                                   cfg->analogs[index].flag);
                                    
                    write_file_gdb(fd,strline,strlenr,counter);
    }
    
    for (index = 0; index < cfg->digit_count; index++) {

                 memset(strline, '\0', strmaxl);

                 strlenr = sprintf(strline, "%d,%s,%d\n",

                                    cfg->digits[index].index,

                                    cfg->digits[index].name,

                                    cfg->digits[index].state);

                  write_file_gdb(fd,strline,strlenr,counter);

    }



          memset(strline, '\0', strmaxl);

          strlenr = sprintf(strline, "%d\n", cfg->frequency);

          write_file_gdb(fd,strline,strlenr,counter);
        
   

         memset(strline, '\0', strmaxl);

         strlenr = sprintf(strline, "%d\n", cfg->smprate_count);

         write_file_gdb(fd,strline,strlenr,counter);


        memset(strline, '\0', strmaxl);

         strlenr = sprintf(strline, "%d,%d\n", cfg->rate,cfg->point);

         write_file_gdb(fd,strline,strlenr,counter);
  

         memset(strline, '\0', strmaxl);

         strlenr = sprintf(strline, "%s\n", cfg->begin_time);
         write_file_gdb(fd,strline,strlenr,counter);
         memset(strline, '\0', strmaxl);

         strlenr = sprintf(strline, "%s\n", cfg->end_time);

         write_file_gdb(fd,strline,strlenr,counter);
         strlenr = sprintf(strline, "%s\n", cfg->file_type);
         write_file_gdb(fd,strline,strlenr,counter);

         free(strline);
        return 0;
}

int write_cmtr_dat_smpdot_ascii(int pfd,int analog_count, int digit_count,cmtr_dat_smpdot *dot,int* counter)
{
        int strlenr = 0;
        
         int strmaxl = CMTR_STRING_MAX_LENGTH;

         char* strline = (char *)malloc(strmaxl);

         int index = 0;
         int curr_pfd = pfd;
    
        memset(strline, '\0', strmaxl);
         strlenr = sprintf(strline, "%d,%d", dot->index, dot->time);
         write_file_utf(curr_pfd, strline, strlenr, counter);

         for (index = 0; index < analog_count; index++) {
                 memset(strline, '\0', strmaxl);
                 strlenr = sprintf(strline, ",%d", dot->analogs[index]);
                 write_file_utf(curr_pfd, strline, strlenr, counter);
         }

         for (index = 0; index < digit_count; index++) {
                 memset(strline, '\0', strmaxl);
                strlenr = sprintf(strline, ",%d", dot->digits[index]);
                 write_file_utf(curr_pfd, strline, strlenr, counter);
         }

        memset(strline, '\0', strmaxl);
        strlenr = sprintf(strline, "\n");
        write_file_utf(curr_pfd, strline, strlenr, counter);

         free(strline);
    return 0;
}

